package com.wtj.user.controller;

import com.wtj.user.service.MenuService;
import com.wtj.utils.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     * 获取菜单 curl http://localhost:8080/menu/1
     * @return
     */
    @GetMapping("/menu")
    @PreAuthorize("hasAuthority('system:dept:lists')")
    public ResponseResult<List<String>> getMenu(@RequestParam("userId") Long userId) {
        // 验证用户ID是否有效
        if (userId == null || userId <= 0) {
            return new ResponseResult<>(400, "Invalid userId");
        }

        // 获取用户菜单权限
        List<String> permissions = menuService.selectPermsByUserId(userId);
        if (permissions == null || permissions.isEmpty()) {
            return new ResponseResult<>(404, "User not found or user has no permissions");
        }
        return new ResponseResult<>(200, permissions);

    }
}
